package com.ldg.rmq.listener;

import com.alibaba.fastjson.JSON;
import com.ld.utils.SmsUtil;
import com.ldg.rmq.pojo.UserInfo;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: Liudao
 * @Date: 2022-10-09-17:44
 * @Description:
 */
@Component
@RabbitListener(queues = "codeMsg")
public class MyRmqCodeListener{
    @Autowired
    private SmsUtil smsUtil;
    @SneakyThrows
    @RabbitHandler
    public void onMessage(String userInfo) {

        //接收到消息并转换为UserInfo对象
        UserInfo user = JSON.parseObject(userInfo, UserInfo.class);
        System.out.println("user = " + user);
        //发送消息给阿里云
        smsUtil.sendSms(user.getAccessKey(),
                user.getAccessKeySecret(),
                user.getPhone(),
                user.getSignName(),
                user.getTemplateCode(),
                user.getTemplateParam());
    }
}
